
from core.base import Editor
from core import cmds

from ui.widgets import QSliderField, QListEditor, ImageWidget, PathFinder
from PyQt4.QtCore import *
from PyQt4.QtGui import *




class IntEditor(Editor):
    attribute = ni.Int
    widget = QSliderField
    display = None
    
    def setup(self):
        ''' '''
        self.widget.setMinimum(self.data.attr.min)
        self.widget.setMaximum(self.data.attr.max)
        self.widget.setValue(self.data.get())
        #QObject.connect(self.widget, SIGNAL("valueChanged"), self.on_slider_change)
        QObject.connect(self.widget, SIGNAL("editingFinished"), self.slider_edited)
        self.data.change.connect(self.on_data_changed)
        
    def on_data_changed(self, value):
        print 'DATA CHANGED', value
        self.widget.setValue(value)
        
    def slider_edited(self):
        self.data.set(int(self.widget.value()))
        cmds.save()

'''
class ImageEditor(Editor):
    attribute = ni.Image
    widget = None
    display = ImageWidget
    
    def setup(self):
        self.display.load(str(self.data.get()))
        self.data.change.connect(self.data_change)
        
    def data_change(self, value):
        self.display.load(self.data.get())
'''

class ChoicesEditor(Editor):
    attribute = ni.Choices
    widget = QComboBox
    display = None
    
    def setup(self):
        QObject.connect(self.widget, SIGNAL("activated (int)"), self.index_changed)
        model = QStandardItemModel()
        self.widget.setModel(model)
        self.setup_choices()
        self.data.change.connect(self.data_change)
        self.data_change()
        
    def setup_choices(self):
        model = self.widget.model()
        #curr_data = self.data.get()
        for index, data in enumerate(self.data.attr.choices):
            item = QStandardItem()
            item.setText(str(data))
            item._data = data
            model.appendRow(item)
            
            #if data == curr_data:
            #    self.widget.setCurrentIndex(index)
        
    def data_change(self, *args):
        model = self.widget.model()
        curr_data = self.data.get()
        for row in range(model.rowCount()):
            item = model.item(row, 0)
            if item._data == curr_data:
                self.widget.setCurrentIndex(row)
        
    def index_changed(self, index):
        model = self.widget.model()
        print 'index changed', model.item(index, 0)._data
        self.data.set(model.item(index, 0)._data)
        
class PathEditor(Editor):
    attribute = ni.Path
    widget = PathFinder
    display = None
    
    def setup(self):
        self.widget.setPath(str(self.data.get()))
        QObject.connect(self.widget, SIGNAL("pathChanged"), self.widget_edited)
        self.data.change.connect(self.data_change)
        
    def data_change(self, value):
        self.widget.setPath(str(self.data.get()))
        
    def widget_edited(self):
        print 'WIDGET EDITIED'
        self.data.set(self.widget.path())
        cmds.save()


class ReferenceEditor(Editor):
    attribute = ni.Reference
    widget = QComboBox
    
    def setup(self):
        print 'setup reference editor', self.data.attr.type
        self._components = {}
        for comp in cmds.ls(self.data.attr.type):
            self.widget.addItem(str(comp))
            self._components[str(comp)] = comp
        QObject.connect(self.widget, SIGNAL("activated (const QString&)"), self.on_component_selected)
        self.on_data_change(self.data)
        self.data.change.connect(self.on_data_change)
        
    def on_data_change(self, value):
        comp_name = str(self.data.get())
        for index in range(self.widget.count()):
            if str(self.widget.itemText(index)) == comp_name:
                self.widget.setCurrentIndex(index)
                break
    
    def on_component_selected(self):
        component = self._components[str(self.widget.itemText(self.widget.currentIndex()))]
        self.data.set(component)
        cmds.save()

  
class TreeEditor(Editor):
    attribute = ni.Tree
    widget = QListEditor
    
    def setup(self):
        QObject.connect(self.widget, SIGNAL("childCreated"), self.on_child_created)
        
    def on_child_created(self, parent_handler, item):
        parent_handler.append(item)
        
    def on_tree_change(self, tree):
        self.widget.set_tree(self.data.get())
        cmds.save()

class BoolEditor(Editor):
    attribute = ni.Bool
    widget = QCheckBox

    def setup(self):
        self.widget.setChecked(bool(self.data.get()))
        QObject.connect(self.widget, SIGNAL("toggled(bool)"), self.on_state_change)
        self.data.change.connect(self.on_data_change)
        
    def on_data_change(self, value):
        self.widget.setChecked(bool(self.data.get()))
        
    def on_state_change(self, state=0):
        with self.data.change.blocking(self.on_data_change):
            self.data.set( self.widget.isChecked() )
        cmds.save()

class StringEditor(Editor):
    attribute = ni.String
    widget = QLineEdit
    
    def setup(self):
        self.widget.setText(str(self.data.get()))
        self.widget.setInputMask(self.data.attr.mask)
        QObject.connect(self.widget, SIGNAL("editingFinished()"), self.on_text_change)
        #QObject.connect(self.widget, SIGNAL("returnPressed()"), self.on_text_change)
        self.data.change.connect(self.on_data_change)
    
    
    
    def on_data_change(self, value):
        self.widget.setText(self.data.get())
        
    def on_text_change(self):
        #with self.data.change.blocking(self.on_data_change):
        #self.data.set( self.widget.isChecked() )
        self.data.set(str(self.widget.text()))
        cmds.save()
    
class FunctionEditor(Editor):
    attribute = ni.Function
    widget = QLineEdit
    
    def setup(self):
        func_string = self.data.stored_data()
        print 'FUNCTION EDITOR', func_string
        if func_string: 
            self.widget.setText(func_string)
        QObject.connect(self.widget, SIGNAL("editingFinished()"), self.on_text_change)
        #self.data.change.connect(self.on_data_change)
        
    def on_data_change(self, value):
        self.widget.setText(self.data.stored_data())
        
    def on_text_change(self):
        self.data.set(str(self.widget.text()))
        cmds.save()
        
class SlotEditor(Editor):
    attribute = ni.Slot
    widget = QComboBox
    
    def setup(self):
        for name in cmds.signal_names():
            self.widget.addItem(name)
        QObject.connect(self.widget, SIGNAL("activated (const QString&)"), self.on_signal_selected)
        self.on_data_change(self.data)
        self.data.change.connect(self.on_data_change)
        
    def on_data_change(self, value):
        signal_name = self.data._data
        for index in range(self.widget.count()):
            if str(self.widget.itemText(index)) == signal_name:
                self.widget.setCurrentIndex(index)
                break
        
    def on_signal_selected(self):
        self.data.set(str(self.widget.itemText(self.widget.currentIndex())))
        cmds.save()
        
        
'''
class ListEditor(Editor):
    attribute = ni.List
    widget = QListEditor
    
    def setup(self):
        self.widget.set_items(self.data._data)
        QObject.connect(self.widget, SIGNAL("addItem"), self.add_item)
        QObject.connect(self.widget, SIGNAL("remItem"), self.rem_item)
        self.data.change.connect(self.data_change)
        
    def data_change(self, value):
        self.widget.set_items(self.data._data)
        
    def add_item(self):
        self.data.add_item()
        
    def rem_item(self, item):
        self.data.remove(item)
'''    
    
    
    
    
    
    
    